﻿using System;
using DatabaseGroup.SSIS.SourceAdapters;
using System.Collections.Generic;

namespace DatabaseGroup.UnitTests.SSIS.SourceAdapters
{
    /// <summary>
    /// Implements dummy IComponentBufferService functionality to aid testing
    /// </summary>
    public class ComponentBufferServiceTestImpl : IComponentBufferService
    {
        private string [] _columnNames = new string[0];
        private object[] _columnData = new object[0];

        private bool _errorBufferUsed = false;
        private bool _logBufferUsed = false;

        private int _rowCount = 0;
        private int _errorRowCount = 0;
        private int _logRowCount = 0;

        private Exception _exceptionToFire = null;

        /// <summary>
        /// Initializes a new ComponentBufferServiceTestImpl using column names and errorBufferUsed flag
        /// </summary>
        /// <param name="columnNames">The names of the mock columns to setup</param>
        /// <param name="errorBufferUsed">Indicates the the component buffer will have an error output buffer present</param>
        /// <param name="logBufferUsed">Indicates the the component buffer will have a log output buffer present</param>
        public ComponentBufferServiceTestImpl(string[] columnNames, bool errorBufferUsed, bool logBufferUsed)
        {
            _columnNames = columnNames;
            _errorBufferUsed = errorBufferUsed;
            _logBufferUsed = logBufferUsed;
            _columnData = new object[columnNames.Length];
        }

        /// <summary>
        /// Gets or sets the number of standard output rows processed
        /// </summary>
        public int RowCount
        {
            get { return _rowCount; }
            set { _rowCount = value; }
        }

        /// <summary>
        /// Gets or sets the number of error output rows processed
        /// </summary>
        public int ErrorRowCount
        {
            get { return _errorRowCount; }
            set { _errorRowCount = value; }
        }

        /// <summary>
        /// Gets or sets the number of log output rows processed
        /// </summary>
        public int LogRowCount
        {
            get { return _logRowCount; }
            set { _logRowCount = value; }
        }

        /// <summary>
        /// Gets or sets the exception to throw when SetColumnData is called
        /// </summary>
        public Exception ExceptionToFire
        {
            get { return _exceptionToFire; }
            set { _exceptionToFire = value; }
        }

        /// <summary>
        /// Gets the data held at a specific column
        /// </summary>
        /// <param name="index">The zero-based column index to retrieve data for</param>
        /// <returns>The column data held for the specified column</returns>
        public object GetColumnData(int index)
        {
            return _columnData[index];
        }

        #region IComponentBufferService Members

        public void AddRow()
        {
            _rowCount++;
        }

        public bool LogOutputUsed
        {
            get
            {
                return _logBufferUsed;
            }
        }

        public void AddErrorRow(int errorCode, string errorMessage, long lineNumber, string rowData, bool isInvalid, string columnName, string columnData)
        {
            _errorRowCount++;
        }

        public void AddLogRow(int errorCode, string errorMessage, long lineNumber, string columnName, string columnData)
        {
            _logRowCount++;
        }

        public int ColumnCount
        {
            get { return _columnNames.Length; }
        }

        public bool ErrorOutputUsed
        {
            get { return _errorBufferUsed; }
        }

        public AuditingConfiguration LogAuditingConfig { get; set; }

        public AuditingConfiguration ErrorAuditingConfig { get; set; }

        public void RemoveRow()
        {
            _rowCount--;
        }

        public void SetColumnData(int index, string columnData)
        {
            if (_exceptionToFire != null)
            {
                throw _exceptionToFire;
            }
            _columnData[index] = columnData;
        }

        public void SetNull(int index)
        {
            _columnData[index] = null;
        }

        #endregion
    }
}
